package oack1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class StringParse {

	static String delimiter = ",";
	static String[] temp;
	
	public static int DetailParser(String input) throws SQLException {
		temp =  input.split(delimiter);
		
		Connection conn = null;
		
        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        int count;
        count = s.executeUpdate (
                "INSERT INTO UserDetail (FirstName, LastName, Gender, LinkedIn, Profession, IIITMail)"
                + " VALUES"
                + "('" + temp[1] + "', '" + temp[2] +"', '" + temp[3] +"', '" + temp[4] + "', " +
                		"'" + temp[5] + "', '" + temp[6] + "')");
        s.close ();
        int count1 = LoginInput("secret," + temp[6] + "," + temp[7]);
        if(count==1 && count1==1)
        	return 1;
        else
        	return 0;
		
	}
	
	public static int LoginParser(String input) throws SQLException {
		temp = input.split(delimiter);
		
		Connection conn = null;
		
        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        int count;
        
        ResultSet rs = s.executeQuery ("SELECT * FROM LoginDetail"); 
        while(rs.next()){
        	if(rs.getString("Username").equals(temp[1])){
        		if(rs.getString("Password").equals(temp[2])){
        			return 1;
        		}
        	}
        }
        
        	return 0;
		
	}
	
	public static int LoginInput(String input) throws SQLException {
		temp = input.split(delimiter);
		
		Connection conn = null;
		
        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        int count;
        count = s.executeUpdate (
                "INSERT INTO LoginDetail (Username,Password)"
                + " VALUES"
                + "('" + temp[1] + "', '" + temp[2] + "')");
        s.close ();
        if(count==1)
        	return 1;
        else
        	return 0;
        
		
	}

	public static int EventParser(String input) throws SQLException {
		temp = input.split(delimiter);
		
		Connection conn = null;
		
        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        int count;
        count = s.executeUpdate (
                "INSERT INTO EventDetail (Name, Date, Time, CreatedBy, Venue, Tags)"
                + " VALUES"
                + "('" + temp[1] + "', '" + temp[2] +"', '" + temp[3] +"', '" + temp[4] + "', " +
                		"'" + temp[5] + "', '" + temp[6] + "')");
        s.close ();
        if(count==1)
        	return 1;
        else
        	return 0;
		
	}
	
	public static ArrayList<String> TagParser(String input) throws SQLException {
		
		Connection conn = null;
		ArrayList<String> result = new ArrayList<String>();
        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        
        ResultSet rs=null;
        rs = s.executeQuery ("SELECT * FROM EventDetail");
        
        while(rs.next()){
        	String[] tagsArr = rs.getString("Tags").split("#");
        	if(arrayToString(tagsArr, " # ").contains(input)){
        		String tempResult = "Event Name - " + rs.getString("Name") + "\n" + 
        				"Event Date - " + rs.getString("Date") + "\n" + 
        				"Event Time - " + rs.getString("Time") + 
        				"Event Venue - " + rs.getString("Venue");
        		result.add(tempResult);
        	}
        }
        s.close();
        	return result;
		
	}
	
	public static String arrayToString(String[] a, String separator) {
	    StringBuffer result = new StringBuffer();
	    if (a.length > 0) {
	        result.append(a[0]);
	        for (int i=1; i<a.length; i++) {
	            result.append(separator);
	            result.append(a[i]);
	        }
	    }
	    return result.toString();
	}
}
